package application.client.gui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import middleware.datacontainer.searchmedium.MediumContainer;

import application.controller.client.GuiController;

public class CloseTabButton extends JPanel implements ActionListener {
	  
	private JTabbedPane pane;
	private Object tab;
	  
	  public CloseTabButton(JTabbedPane pane, int index, Object o) {
	    
		  tab=o;
		  this.pane = pane;
	    setOpaque(false);
	    setLayout(new BorderLayout());
	    
	    add(new JLabel(
	        pane.getTitleAt(index),
	        pane.getIconAt(index),
	        JLabel.RIGHT),BorderLayout.CENTER);
	    
	    Icon closeIcon = new CloseIcon();
	    JButton btClose = new JButton(closeIcon);
	    btClose.setPreferredSize(new Dimension(
	        closeIcon.getIconWidth(), closeIcon.getIconHeight()));
	    add(btClose,BorderLayout.EAST);
	    btClose.addActionListener(this);
	    pane.setTabComponentAt(index, this);
	  }
	  
	  @Override
	  public void actionPerformed(ActionEvent e) {
	    int i = pane.indexOfTabComponent(this);
	    if (i != -1) {
	     pane.remove(i);
	     GuiController.getInstance().openedTabs.remove(tab);
	    }
	   
	  }
	
	
	class CloseIcon implements Icon {
		@Override	
		  public void paintIcon(Component c, Graphics g, int x, int y) {
		    g.setColor(Color.RED);
		    g.drawLine(6, 6, getIconWidth() - 7, getIconHeight() - 7);
		    g.drawLine(getIconWidth() - 7, 6, 6, getIconHeight() - 7);
		  }
		  public int getIconWidth() {
		    return 17;
		  }
		  public int getIconHeight() {
		    return 17;
	  }
	
	}
}